package game.Sprites;

import game.Helper;
import game.Vector3;
import javafx.scene.paint.Color;

import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;


public class BloodSplash extends VanishingSprite {
    double speed;

    public BloodSplash(Vector3 position, double rotation) throws Exception {
        super(position, rotation, 1.0, SpriteResources.getBloodImg(), Color.DARKRED, 30.0);
        this.position.z = -1.0;
        Random rnd = new Random(System.nanoTime());
        this.speed = rnd.nextDouble()*5.0;
    }


    public void moveSplash(Vector3 newPos) {
        Vector3 localMapPos = Helper.mainScene.getMap().globalToLocal(newPos);

        Ellipse2D.Double splashCircle = new Ellipse2D.Double(localMapPos.x - size / 2, localMapPos.y - size / 2, size, size);

        boolean collides = false;
        for (int x = (int) localMapPos.x - 1; x <= (int) localMapPos.x + 1; x++) {
            for (int y = (int) localMapPos.y - 1; y <= (int) localMapPos.y + 1; y++) {
                Rectangle2D.Double tileRectangle = new Rectangle2D.Double(x, y, 1.0, 1.0);
                if (!Helper.mainScene.getMap().isWalkable(x, y) && splashCircle.intersects(tileRectangle)) {
                    collides = true;
                    break;
                }

            }
        }

        if (!collides) {
            this.setPosition(newPos);
        }
    }

    @Override
    public void update(double deltaTime) {
        super.update(deltaTime);

        if(this.speed > 0.01){
            Vector3 newpos = this.position.add(
                    new Vector3(Math.cos(rotation), Math.sin(rotation), 0.0).multiply(speed).multiply(deltaTime)
            );

            moveSplash(newpos);
        }

        this.speed = this.speed * 0.95;
    }
}
